import 'dart:convert';
import 'package:flutter/foundation.dart';

export 'package:dio/dio.dart';

// 必须是顶层函数
_parseAndDecode(String response) {
  return jsonDecode(response);
}
parseJson(String text) {
  return compute(_parseAndDecode, text);
}

/// 子类需要重写
abstract class BaseResponseData {
  int success;
  var errors;
  int status;
  int code;
  var message;
  var data;

  BaseResponseData({ this.message, this.data});

  @override
  String toString() {
    return 'BaseRespData{errcode: $status, errmsg: $errors';
  }
}

/// 接口的code没有返回为true的异常
class NotSuccessException implements Exception {
  
  var errors;
  int status;
  var message;
  NotSuccessException.fromRespData(BaseResponseData respData) {
    errors = respData.errors;
    status = respData.status;
    message = respData.message;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['status'] = this.status;
    data['errors'] = this.errors;
    data['message'] = this.message;
    return data;
  }

  @override
  String toString() {
    return "${this.toJson()}";
  }

}

